package cn.b.sky.mes.em.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.Failure;
import cn.b.sky.mes.em.model.condition.FailureCondition;
import cn.b.sky.mes.em.service.FailureService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/failure/*")
public class FailureController extends SecurityController {
  @Resource
  FailureService failureService;

  @RequestMapping("failureList")
  public String failureList(Model model, @ModelAttribute("condition") FailureCondition failure, String navTabId) {
    failure.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(failure.getOrderField())) {
      failure.setOrderField("createDateTime");
    }
    List<Failure> failureList = failureService.selectFailureByCondition(failure);
    int count = failureService.selectFailureCount(failure);
    failure.setData(failureList);
    failure.setTotalCount(count);
    model.addAttribute("page", failure);
    model.addAttribute(NAVTABID, navTabId);
    return "em/failure/failure_list";
  }

  @RequestMapping("addFailure")
  public String addFailure(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "em/failure/failure_add";
  }

  @RequestMapping("modifyFailure")
  public String modifyFailure(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    Failure failure = failureService.selectFailureByCode(getSiteCode(), id);
    model.addAttribute("failure", failure);
    return "em/failure/failure_add";
  }

  @RequestMapping("validateFailureCode")
  @ResponseBody
  public boolean validateFailureCode(String failureCode, Model model) {
    if (!StringUtils.isEmpty(failureCode)) {
      return null == failureService.selectFailureByCode(getSiteCode(), failureCode);
    }
    return true;
  }

  @RequestMapping("saveFailure")
  @ResponseBody
  public DWZResult saveFailure(Model model, String navTabId, Failure failure, String id, HttpServletRequest request) {
    if (failure == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    failure.setSiteCode(getSiteCode());
    setCreateInfo(failure);
    if (StringUtils.isEmpty(id)) {
      failureService.insertFailure(failure);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    } else {
      failureService.updateFailure(failure);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);

    }
  }

  @RequestMapping("viewFailure")
  public String viewFailure(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    Failure failure = failureService.selectFailureByCode(getSiteCode(), id);
    model.addAttribute("failure", failure);
    return "em/failure/failure_view";
  }

  @RequestMapping("deleteFailure")
  @ResponseBody
  public DWZResult deleteFailure(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = failureService.deleteFailure(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping(value = "failureLookup")
  public String categoryLookup(String navTabId, Model model, @ModelAttribute("condition") FailureCondition condition) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    condition.setLockFlag(0);
    List<Failure> list = failureService.selectFailureByCondition(condition);
    int count = failureService.selectFailureCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    model.addAttribute("page", condition);
    return "em/failure/failure_lookup";
  }

  @RequestMapping(value = "failureSuggest")
  @ResponseBody
  public Object failureSuggest(String navTabId, Model model, @ModelAttribute("condition") FailureCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    return failureService.selectFailureForSuggest(condition);
  }

  @RequestMapping(value = "checkFailureCode")
  @ResponseBody
  public boolean checkFailureCode(String navTabId, Model model, String failureCode) {
    if (StringUtils.isEmpty(failureCode)) {
      return false;
    } else {
      Failure failure = failureService.selectFailureByCode(getSiteCode(), failureCode);
      return failure != null && failure.getLockFlag() != 1;
    }
  }
}
